import { fileURLToPath, URL } from "node:url";

import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import vueJsx from "@vitejs/plugin-vue-jsx";

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd());
  return {
    plugins: [vue(), vueJsx()],
    base: "./",
    server: {
      port: 8080, // 端口号
      open: true, // 自动打开浏览器
      host: "localhost", //主机名
      proxy: {
        [env.VITE_APP_BASE_API]: {
          target:
            "https://mock.mengxuegu.com/mock/66385062cab9671f88bd3035/example",
          changeOrigin: true,
          rewrite: (path) =>
            path.replace(new RegExp("^" + `${env.VITE_APP_BASE_API}`), ""),
          // rewrite: (path) => path.replace(/`^${env.VITE_APP_BASE_API}`/, ""),
        },
        
        // [import.meta.env.VITE_APP_BASE_API]: {
        //   target:
        //     "https://mock.mengxuegu.com/mock/66385062cab9671f88bd3035/example",
        //   changeOrigin: true,
        //   rewrite: (path) => path.replace(/^\import.meta.env.VITE_APP_BASE_API/, ""),
        // },
      },
    },
    resolve: {
      alias: {
        "@": fileURLToPath(new URL("./src", import.meta.url)),
      },
    },
  };
});
